package com.kch.system.service.impl;

import com.kch.system.domain.Meeting;
import com.kch.system.domain.MeetingApplicants;
import com.kch.system.domain.SysUser;
import com.kch.system.mapper.MeetingApplicantsMapper;
import com.kch.system.mapper.MeetingMapper;
import com.kch.system.req.CommonReq;
import com.kch.system.service.IMeetingApplicantsService;
import com.kch.system.vo.MeetingMemberVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MeetingApplicantsServiceImpl implements IMeetingApplicantsService {

    @Autowired
    private MeetingApplicantsMapper meetingApplicantsMapper;
    @Autowired
    private MeetingMapper meetingMapper;

    @Override
    public List<MeetingMemberVO> selectByMeetingId(MeetingApplicants applicants) {
        return meetingApplicantsMapper.selectByMeetingId(applicants);
    }

    @Override
    public int updateById(MeetingApplicants applicants) {
        //修改报名表审核状态
        MeetingApplicants exist = meetingApplicantsMapper.selectByPrimaryKey(applicants.getId());
        exist.setUpdateTime(new Date());
        exist.setUpdateBy(applicants.getUpdateBy());
        exist.setReviewStatus(applicants.getReviewStatus());
        //如果是通过，则修改会议的人数
        if("1".equals(applicants.getReviewStatus())){
            Meeting meeting = meetingMapper.selectByPrimaryKey(exist.getMeetingId());
            meeting.setMemberPass(meeting.getMemberPass() + 1);
            meeting.setUpdateTime(new Date());
            meeting.setUpdateBy(applicants.getUpdateBy());
            meetingMapper.updatePassMember(meeting);
        }
        return meetingApplicantsMapper.updateByPrimaryKey(exist);
    }

    @Override
    public Map<Integer, Integer> checkUserReg(List<Integer> meetingIds, Integer userId) {
        List<MeetingApplicants> meetingApplicants = meetingApplicantsMapper.selectByMeetingIds(meetingIds, userId);
        return meetingApplicants.stream().collect(Collectors.toMap(MeetingApplicants::getMeetingId, MeetingApplicants::getUserId, (e1, e2) -> e1));
    }

    @Override
    public int application(CommonReq req, SysUser sysUser) {
        Meeting meeting = meetingMapper.selectByPrimaryKey(req.getId());
        if(ObjectUtils.isEmpty(meeting)){
            throw new RuntimeException("传入参数有误，对应记录不存在");
        }
        meeting.setMemberEntry(meeting.getMemberEntry() + 1);
        meeting.setUpdateBy(sysUser.getLoginName());
        meeting.setUpdateTime(new Date());
        meetingMapper.updateEntryMember(meeting);

        MeetingApplicants applicants = new MeetingApplicants();
        applicants.setMeetingId(req.getId());
        applicants.setUserId(sysUser.getUserId().intValue());
        applicants.setReviewStatus("0");
        applicants.setRegTime(new Date());
        applicants.setCreateBy(sysUser.getLoginName());
        applicants.setCreateTime(new Date());
        return meetingApplicantsMapper.insert(applicants);
    }
}
